# -*- coding: utf-8 -*-
import cv2
import tkinter as tk
import windnd
from tkinter.messagebox import showinfo
from PIL import Image, ImageTk
# # self.icon = wx.Icon(name="C:/Users/admin/Desktop/testGUI/bb.png", type=wx.BITMAP_TYPE_PNG)
# self.icon = wx.Icon(name="./pic/bb.png", type=wx.BITMAP_TYPE_PNG)
# self.SetIcon(self.icon)
# self.Show(True)



# root = tk.Tk()
#
# # 打开指定图片，缩放至自定尺寸
# def get_image(filename, width, height):
#     im = Image.open(filename).resize((width, height))
#     return ImageTk.PhotoImage(im)
#
#
# def dragged_files(files):
#     root.destroy()
#     msg = '\n'.join((item.decode('gbk') for item in files))
#     print(msg)
#     # showinfo("拖拽文件路径", msg)
#     input_image = cv2.imread(msg, -1)
#     cv2.imshow("test", input_image)
#     cv2.waitKey(0)
#
# if __name__ == '__main__':
#
#     windnd.hook_dropfiles(root, func=dragged_files)
#     root.mainloop()


from tkinter import *  # Avoid this and use import tkinter as tk
from PIL import Image, ImageTk

root = Tk()
root['bg'] = 'black'


def callback(e):

    x, y = canvas_to_image_cords(canvas=canvas, x=e.x, y=e.y, image=img,
                                 tagOrId='img')  # Can also pass img_tag as tagOrId
    print(x, y)


def canvas_to_image_cords(canvas: Canvas, x: int, y: int, image: PhotoImage, tagOrId=''):
    anchor = 'nw'

    # if tagOrId:
    #     anchor = canvas.itemcget(tagOrId, 'anchor')

    w, h = canvas.winfo_reqwidth(), canvas.winfo_reqheight()
    if anchor == 'center':
        img_xpos, img_ypos = image.width() / 1, image.height() / 1
        start_x, start_y = img_xpos - w / 1, img_ypos - h / 1
    elif anchor == 'nw':
        start_x, start_y = 0, 0
    req_x, req_y = start_x + x, start_y + y
    return req_x, req_y


img = ImageTk.PhotoImage(Image.open('C:/Users/admin/Desktop/dt/test.bmp'))  # 10000x9000 pixel image
# canvas = Canvas(root, highlightthickness=0)
# canvas.pack(padx=0, pady=0)

canvas = tk.Canvas(root, height=800, width=800)
img_tag = canvas.create_image(0, 0, image=img)
canvas.pack()
# img_tag = canvas.create_image(487, 391, image=img, tag='img')  # By default anchor is center

canvas.bind('<1>', callback)

root.mainloop()
